//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/10.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        registerNotification()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
//         WelcomeViewController()
        window?.rootViewController = defaultRootViewController()
        
        window?.makeKeyAndVisible()
        
        setThemeColor()
        
        print(UserAccount.loadUserAccount())
        
        return true
    }
    
    
    private func registerNotification () {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "chouseRootVC:", name: WBSwitchRootVC, object: nil)
      
    }
    
    @objc private func chouseRootVC(n : NSNotification) {
        
        print(n)
        
//        if n.object != nil {
//            
//            window?.rootViewController = WelcomeViewController()
//            
//        } else {
//            window?.rootViewController = MainViewController()
//        }
        
        window?.rootViewController = (n.object == nil ? MainViewController() : WelcomeViewController())
        
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private func defaultRootViewController() ->UIViewController {
        return UserAccountViewModel().userLogin ? WelcomeViewController():MainViewController()
    }

    private func setThemeColor () {
        
        //设置导航控制器的全局色,尽早设置,必须在视图实例化之前设置
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

